package client;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.omg.CORBA.ORB;

import libraryCORBA.libraryCommand;
import libraryCORBA.libraryCommandHelper;

public class RemoteLibraryHelper {

	public static libraryCommand createRemoteLibraryInstance() throws IOException {
		ORB orb = ORB.init(new String[]{}, null);

		String iorFilename = System.getProperty("user.home") + System.getProperty("file.separator") + "ior";
		System.out.println("Loading IOR from: " + iorFilename);
		String ior = readIorFromFile(iorFilename);
		//System.out.println(ior);
		
		org.omg.CORBA.Object obj = orb.string_to_object(ior);
		libraryCommand library = libraryCommandHelper.narrow(obj);

		return library;
	}
	
	public static String readIorFromFile(String filename) throws IOException {
		FileReader fileReader = new FileReader(filename);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String ior = bufferedReader.readLine();
		bufferedReader.close();
		return ior;
	}
	
	
}
